/**
 * @author caslyn
 */
$(document).ready(function(){
	$("#dialog_newnotice").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				if(InvalidNewNotice()){
					exit;
				}
				temp = $('#new_notice_content').val();
				temp = temp.replace(/\n/g,"<br>");
				$.getJSON("json.php", { "task": "addNewNotice",
				"pageid":$('#pageid').val(), "subject": $('#new_notice_subject').val(),
				"content": temp, "schoolid": $('#schoolid').val(), "referenceid": $('#referenceid').val() } , 
					function(data){
						$('#noticelist').prepend('<span id="spnnotice' + data.id + '"><b>' + data.subject + 
						'</b><br/><span>' +
						data.content + '</span> ' +
						WriteActionButton("Edit", "editNotice(" + data.id + ")") + ' ' +
						WriteActionButton("Delete", "deleteNotice(" + data.id + ")") +
						'<hr/></span>' );
//							alert('Save success. ');
						$("#dialog_newnotice").dialog('close');
					}
				);
//					$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#new_notice_subject').val('');
			$('#new_notice_content').val('');
		}
	});

	$("#dialog_editnotice").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				if(InvalidNotice()){exit;}
				temp = $('#edit_notice_content').val();
				temp = temp.replace(/\n/g,"<br>");
				$.getJSON("json.php", { "task": "saveNewNotice", "id": $('#edit_notice_id').val(),
				"pageid": $('#pageid').val(), "subject": $('#edit_notice_subject').val(),
				"content": temp, "schoolid": $('#schoolid').val() } , 
					function(data){
//							spanname = '#spnnotice' + data.id;
						$('#spnnotice' + data.id).children('span').html(data.content);
						$('#spnnotice' + data.id).children('b').html(data.subject);
						alert('Save success. ');
						$("#dialog_editnotice").dialog('close');
					}
				);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#edit_notice_subject').val('');
			$('#edit_notice_content').val('');
		}
	});
	
	$('#addNewNotice').click(function() {
		$('#dialog_newnotice').dialog('open');
	});
	
});

function editNotice(nid){
	// load data from json
//	alert('Editing notice '+ id);
		temp = $('#spnnotice'+nid).children('span').html();
		temp = temp.replace(/<br>/g, "\n");
		$('#edit_notice_subject').val($('#spnnotice'+nid).children('b').html());
		$('#edit_notice_content').val(temp);
		$('#edit_notice_id').val(nid);
		// open edit dialog
		$('#dialog_editnotice').dialog('open');
}

function deleteNotice(noticeid){
	// ask user if want to delete
	var answer = confirm("Do you really want to delete this notice?");
	if(answer == true){
	// perform ajax  request
		$.post("json.php",{task:"deleteNotice",id:noticeid},
		function(data){
			if(data=="1"){
				alert("Notice deleted.");
				$('#spnnotice'+noticeid).remove();
			}
		});			
	}
	// show success.
}

function InvalidNewNotice(){
	if ( $('#new_notice_content').val().length < 1 ){ alert('Content cannot be empty.'); return true; };
	if ( $('#pageid').val().length < 1 ){ alert('Page ID cannot be empty.'); return true; };
	if ( $('#new_notice_subject').val().length < 1 ){ alert('Subject cannot be empty.'); return true; };
	if ( $('#schoolid').val().length < 1 ){ alert('Schiil ID cannot be empty.'); return true; };
	if ( $('#referenceid').val().length < 1 ){ alert('Reference ID cannot be empty.'); return true; };
	return false;
}

function InvalidNotice(){
	if ( $('#edit_notice_id').val().length < 1 ){ alert('Notice ID cannot be empty.'); return true; };
	if ( $('#edit_notice_content').val().length < 1 ){ alert('Content cannot be empty.'); return true; };
	if ( $('#pageid').val().length < 1 ){ alert('Page ID cannot be empty.'); return true; };
	if ( $('#schoolid').val().length < 1 ){ alert('Schiil ID cannot be empty.'); return true; };
	if ( $('#edit_notice_subject').val().length < 1 ){ alert('Subject cannot be empty.'); return true; };
	return false;
	
}
